#!/usr/bin/env python
# coding=utf-8

import os, sys, re, traceback, logging
import urllib
import time
import datetime
import getopt
import urllib2
from Route import *


vacation_info_re_pre = re.compile(r'''
        <line>(.*?)<\s*/\s*line>
        ''', re.X|re.S)

xml_cdata_node_re = re.compile(r'''
        <\s*(\w+)\s*>\s*<!\[CDATA\[\s*(.*?)\s*]]>\s*<\s*/\s*\1\s*>
        ''', re.X|re.S)

xml_schedule_re = re.compile(r'''
        <schedule>(.*?)<\s*/\s*schedule>
        ''', re.X|re.S)

vacation_info_re = re.compile(r'''
        ^(.*?)<\s*schedule>
        ''', re.X|re.S)


def do_crawl():
    try:
        server_url = 'http://ctsho.com/ctshoToOyesgo.jsp'
        print server_url
        urls = [
                    'http://ctsho.com/tour/international/tourinfo.do?tourQueryDto.lineId=67653',
'http://ctsho.com/tour/international/tourinfo.do?tourQueryDto.lineId=72033',
'http://ctsho.com/tour/international/tourinfo.do?tourQueryDto.lineId=72374',
'http://ctsho.com/tour/international/tourinfo.do?tourQueryDto.lineId=72334',
'http://ctsho.com/tour/international/tourinfo.do?tourQueryDto.lineId=72335',
'http://ctsho.com/tour/international/tourinfo.do?tourQueryDto.lineId=72338',
'http://ctsho.com/tour/international/tourinfo.do?tourQueryDto.lineId=72339',
'http://ctsho.com/tour/international/tourinfo.do?tourQueryDto.lineId=67653'
                    ]
        html = urllib2.urlopen(server_url).read()
        print len(html)
        for urltemp in urls:
            print html.find(urltemp)
        vacation_info_list = vacation_info_re_pre.findall(html)
        #print vacation_info_list
        for vacation_info_pre in vacation_info_list:
            #print "-----------------------------------------------------"
            if re.search(vacation_info_re, vacation_info_pre):
                vacation_info = re.search(vacation_info_re, vacation_info_pre).group(1)
                vacation_info_dict = dict(xml_cdata_node_re.findall(vacation_info))
            else:
                #print >>sys.stderr, 'cannot '
                continue
            if not vacation_info_dict:
                #print >>sys.stderr, 'cannot match xml_node_re'
                continue
            v = vacation_info_dict
            route = Route()
            route.site = '35'
            route.supplier_url = "http://www.ctsho.com/"
            route.supplier = "中国旅行社总社"
            #print 'title: ' + v.get('title', '')
            route.title = v.get('title', '')
            #print 'url: ' + v.get('url', '')
            route.rout_url = v.get('url', '')
            #调试打印
            
            if route.rout_url in urls:
                print route.rout_url 
            #print 'duration: ' + v.get('duration', '')
            if v.get('duration', '').strip() != 'null':
                route.days = v.get('duration', '')
            #print 'startcity: ' + v.get('startcity', '')
            if v.get('startcity', '').strip() != 'null':
                route.outcity = v.get('startcity', '').strip().split('市')[0]
            #route.sights = v.get('avenue', '')
            #print 'callcenter: ' + v.get('links', '').split('：')[1]
            route.telphone = v.get('links', '').split('：')[1]
            #print 'characteristic: ' + v.get('characteristic', '') + v.get('tips', '')
            if v.get('characteristic', '').strip() != 'null':
                route.detail = v.get('characteristic', '') 
            #print 'departcountry: ' + v.get('departcountry', '')
            if v.get('avenue', '').strip() != 'null':
                route.dest = v.get('avenue', '')
            #print 'guideservice: ' + v.get('guideservice', '')
            if v.get('guideservice', '').strip() != 'null':
                route.guide = v.get('guideservice', '')
            if v.get('type', '').strip() != 'null':
                route.type = v.get('type', '')
            #print 'type: ' +  v.get('type', '')
                
            route.go_t = v.get('deptraffic', '') if v.get('deptraffic', '') != 'null' else '-'
            route.back_t = v.get('rettraffic', '') if v.get('rettraffic', '') != 'null' else '-'

            #print 'go_t: ' +  v.get('deptraffic', '')
            #print 'back_t: ' +  v.get('rettraffic', '')
            if v.get('dates', '').strip() != 'null':
                route.dates = v.get('dates', '').replace(',',';')
            #print 'dates: ' + v.get('dates', '').replace(',',';')
            if v.get('lowprice', '').strip() != 'null':
                route.price = v.get('lowprice', '')
            #print 'lowprice: ' + v.get('lowprice', '').replace(',',';')
            if v.get('lineimg', '').strip() != 'null':
                route.img_url = v.get('lineimg', '')
            #print 'lineimg: ' + v.get('lineimg', '')
            #print 'presonnum: ' + getPerson(vacation_info_pre)
            #route.person = getPerson(vacation_info_pre)
            #print 'plane: ' + getPlane(vacation_info_pre)
#            route.plane = getPlane(vacation_info_pre)
            #print 'dates: ' + getDates(vacation_info_pre)
#            route.dates = getDates(vacation_info_pre)
            route.schedules = getSchedules(vacation_info_pre)
            
            #print >>sys.stderr, 'updating', route.supplier, route.title
            route.updRoute()
            route.updRoute_Schedules()
       
    except:
        print traceback.format_exc(sys.exc_info())

def getSchedules(vacation_info_pre):
     
     plane_info_list = xml_schedule_re.findall(vacation_info_pre)
     schedules = range(0,len(plane_info_list))
     for plane_info in plane_info_list:
        #print
        plane_info_dict = dict(xml_cdata_node_re.findall(plane_info))
        if not plane_info_dict:
            print >>sys.stderr, 'cannot match xml_node_re'
            continue
        v = plane_info_dict
        schedule = Schedule()
        schedule.s_num=v.get('serialnumber','')
        #print 'serialnumber:' + v.get('serialnumber','')
        if v.get('brief', '').strip() != 'null':
            schedule.s_brief=v.get('brief','')
        #print 'brief:' + v.get('brief','')
        if v.get('traffic', '').strip() != 'null':
            schedule.s_traffic=v.get('traffic','')
        #print 'traffic:' + v.get('traffic','')
        if v.get('accommodation', '').strip() != 'null':
            schedule.s_hotel=v.get('accommodation','')
        #print 'accommodation:' + v.get('accommodation','')
        schedules[string.atoi(v.get('serialnumber',''))-1]=schedule
        #print schedule.s_hotel
     return schedules

def usage():
    print '''Usage: %s [OPTIONS]...
Crawl hotel informations.

  -d,  --debug               enable pdb debugger

  -h,  --help                display this help and exit
''' % sys.argv[0]


if __name__ == '__main__':
    script_path = os.path.dirname(os.path.realpath(__file__))
    os.chdir(script_path)
    do_debug = False

    try:
        opts, args = getopt.gnu_getopt(sys.argv[1:], 'hdno:p:', ['help', 'debug', 'no-cache', 'only-cache', 'provider'])
    except getopt.GetoptError:
        usage()
        sys.exit()

    for opt, arg in opts:
        if opt in ('-h', '--help'):
            usage()
            sys.exit()
        elif opt in ('-d', '--debug'):
            do_debug = True
        elif opt in ('-n', '--no-cache'):
            use_cache = False
        elif opt in ('-o', '--only-cache'):
            if arg.lower() in ('no', 'n', '0'):
                only_cache = False
            else:
                only_cache = True
        elif opt in ('-p', '--provider'):
            pass

    if do_debug:
        import pdb
        pdb.set_trace()
    do_crawl()



